﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Core;
using Web.Prop;

namespace Web
{
    public partial class singin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lbIP.Text = "IP Address: " + Request.ServerVariables["REMOTE_ADDR"];
            if (!Page.IsPostBack)
            {
                Session.Remove("UserInfo");
                txUsername.Text = String.Empty;
                txPassword.Text = String.Empty;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);

            if (String.IsNullOrEmpty(txUsername.Text.Trim()))
            {
                Alert.Message(this.Page, "กรุณากรอกชื่อ");
            }
           
            else if (String.IsNullOrEmpty(txPassword.Text))
            {
                Alert.Message(this.Page, "กรุณากรอกรหัสผ่าน");
               
            }

            else
            {
                var o = Dao.USERDao.Init().Select(
                    new USER()
                        {
                            UWD = txUsername.Text.Trim()
                            ,
                            PWD =  Core.Cipher.EncrytData(txPassword.Text.Trim())
                        }
                    );
                try
                {

                    if (o.Count > 0)
                    {
                        if (o.ToList().First().STATUS == ((Int32) Core.Status.Use).ToString())
                        {
                            if (!String.IsNullOrEmpty(o.ToList().First().UID))
                            {
                                Session.Remove("UserInfo");
                                Session["UserInfo"] = o.ToList().First();
                                //
                                Dao.UserlogDao.Init().Add(new Userlog()
                                {
                                    UID = o.ToList().First().UID
                                    ,
                                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                                    ,
                                    UPDATE = DateTime.Now
                                    ,
                                    EVENT = Core.Events.Login.ToString()
                                });
                                //
                                Dao.USERDao.Init().Udate(o.ToList().First().UID
                                    , DateTime.Now
                                    , Request.ServerVariables["REMOTE_ADDR"]);
                                //
                                Response.Redirect("pages/default.aspx");
                            }
                        }
                        else
                        {
                            //
                            Dao.USERDao.Init().Udate(o.ToList().First().UID
                                , DateTime.Now
                                , Request.ServerVariables["REMOTE_ADDR"]);

                            Alert.Message(this.Page, "กรุณาแจ้ง Administration Account เพื่อเปิดสิทธิ์การใช้งานของท่าน");
                        }

                        
                    }
                    else
                    {
                        Alert.Message(this.Page, "ไม่พบข้อมูลนี้ในระบบ");
                    }
                }
                catch (Exception ex)
                {
                      Dao.TransactionlogDao.Init().Add(new Transactionlog()
                            {
                                UID = null
                                ,
                                EVENT = Core.Events.Login.ToString()
                                ,
                                LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                                ,
                                UPDATE = DateTime.Now
                                ,
                                DESCRIPT = ex.Message
                            });


                      Alert.Message(this.Page, "ไม่พบข้อมูลนี้ในระบบ");
                }
              }

            }
        }

}